﻿using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading;
using System.Xml;
using System.Windows.Forms;

namespace PublicTransport
{
    class GpsMapImageGoogle : GpsMapImage
    {
        private string imageRequest;

        public GpsMapImageGoogle(GpsPoint source, GpsPoint destination, int sizeX, int sizeY)
        {
            try
            {
                this.sizeY = sizeY;
                this.sizeX = sizeX;

                xmlRequest = "http://maps.googleapis.com/maps/api/directions/xml?origin=" + source.getCoordinates() + "&destination=" + destination.getCoordinates() + "&mode=walking&sensor=false";
                XmlDocument xDoc = new XmlDocument();

                WebRequest webRequest = WebRequest.Create(xmlRequest);
                webRequest.Timeout = 10000;
                xDoc.Load(XmlReader.Create(new StringReader((new StreamReader(((HttpWebResponse)(webRequest).GetResponse()).GetResponseStream())).ReadToEnd())));
                webRequest.Abort();

                if (xDoc.GetElementsByTagName("status")[0].InnerText.Equals("OK"))
                {
                    GpsPoint upperLeft = new GpsPoint(Double.Parse(xDoc.GetElementsByTagName("northeast")[0].FirstChild.InnerText), Double.Parse(xDoc.GetElementsByTagName("southwest")[0].LastChild.InnerText));
                    GpsPoint bottomRight = new GpsPoint(Double.Parse(xDoc.GetElementsByTagName("southwest")[0].FirstChild.InnerText), Double.Parse(xDoc.GetElementsByTagName("northeast")[0].LastChild.InnerText));

                    zoom = (int)(Math.Log(Math.Min(((double)sizeX) / (bottomRight.X - upperLeft.X), ((double)sizeY) / (upperLeft.Y - bottomRight.Y)) * 1.46025) / Math.Log(2)) - 1;
                    centerPoint = upperLeft.getCenterGpsPoint(bottomRight);
                    imageRequest = "http://maps.google.com/maps/api/staticmap?path=color:0x0000FF80|weight:5|enc:" + xDoc.GetElementsByTagName("overview_polyline")[0].InnerText + "&markers=color:blue%7Clabel:Z%7C" + destination.getCoordinates() + "&size=" + sizeX + "x" + sizeY + "&sensor=false&center=";
                    constructorFinished = true;
                    getImage();
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
                finished = true;
            }
        }

        public override void getImage()
        {
            if (!httpBlock && constructorFinished)
            {
                httpBlock = true;
                finished = false;
                ok = false;
                ThreadStart thread = () =>
                {
                    try
                    {
                        WebRequest webRequest = WebRequest.Create(imageRequest + centerPoint.getCoordinates() + "&zoom=" + zoom + (mapType == 1 ? "&maptype=hybrid" : ""));
                        webRequest.Timeout = 10000;
                        image = new System.Drawing.Bitmap(((HttpWebResponse)webRequest.GetResponse()).GetResponseStream());
                        webRequest.Abort();
                        httpBlock = false;
                        Point posun = degreesToPixels(centerPoint);
                        upperLeftPixels = new Point((int)Math.Round(posun.X - (double)sizeX / 2.0), (int)Math.Round(posun.Y - (double)sizeY / 2.0));
                        ok = true;
                    }
                    catch (Exception ex)
                    {
                        httpBlock = false;
                        error = ex.Message;
                    }
                    finished = true;
                };
                thread.Invoke();
            }
        }
    }
}

